﻿using Common.Contract;
using Common.Contract.Customer.Identifier;
using Common.Contract.Customer.Record;
using Common.Contract.DataCenter.Record;
using Common.Contract.DataCenter.Request;
using Common.Contract.SaleOut.Record;
using Common.Contract.Warranty.Record;
using Common.Service.Command;
using Common.Service.Queue;
using Common.Util;
using DataCenter.Configuration;
using DataCenter.Model.Interface;
using DataCenter.Model.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataCenter.ServiceCommand
{
    public class UpdateWarrantyHistoryServiceCommand : ProviderQueueCommand<IProviderFactory, WarrantyHistoryRecord, ResponseRecord>
    {
        public UpdateWarrantyHistoryServiceCommand(IProviderFactory provider) : base(provider) { }
        protected override void Validate(WarrantyHistoryRecord record)
        {
            if (record == null) throw new ArgumentNullException("request");
        }

        protected override bool OnExecute(ResponseRecord response)
        {
            var rdcHist = Record;
            var obj = new WarrantyHistory()
            {

                Cmdcode = rdcHist.Cmdcode,
                CreateDate = rdcHist.CreateDate,
                EdiFile = rdcHist.EdiFile,
                EdiHead = rdcHist.EdiHead,
                EndUserCell = rdcHist.EndUserCell,
                Model = rdcHist.Model,
                MoMsgbody = rdcHist.Messagebody,
                Moseq = rdcHist.Moseq,
                ReceiveDate = rdcHist.ReceiveDate,
                RespMsg = rdcHist.ResponseMessage,
                RespType = rdcHist.ResponseType,
                SerialNo = rdcHist.SerialNumber,
                ShopCell = rdcHist.ShopCell,
                ShopCode = rdcHist.ShopCode,
                SmsYn = rdcHist.SmsYn.HasValue ? rdcHist.SmsYn.Value.ToBooleanString() : null,
                SoTransferFlag = BooleanString.TRUE,
                SoTransferDate = DateTime.Now,
                SuccessFlag = rdcHist.SuccessFlag.HasValue ? rdcHist.SuccessFlag.Value.ToBooleanString() : null,
            };

            var itemCheck = Providers.WarrantyHistory.RetrieveWarrantyHistory(rdcHist.SerialNumber, rdcHist.ResponseType);
            if (itemCheck == null)
            {
                var resultitem1 = Providers.WarrantyHistory.Add(obj);
            }
            else
            {
                var resultitem2 = Providers.WarrantyHistory.UpdateWarrantyHistory(obj);
            }
            return true;
        }
    }
}
